<?php

namespace Apps\Common\Models;

/**
 * 用户基础信息Model
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class UserBase extends \PhalconPlus\Database\Model
{

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var string
     */
    public $user_account;

    /**
     *
     * @var string
     */
    public $user_password;

    /**
     *
     * @var string
     */
    public $user_nickname;

    /**
     *
     * @var string
     */
    public $user_cover;

    /**
     *
     * @var integer
     */
    public $user_gender;

    /**
     *
     * @var string
     */
    public $user_birthday;

    /**
     *
     * @var string
     */
    public $user_realname;

    /**
     *
     * @var integer
     */
    public $user_district;

    /**
     *
     * @var integer
     */
    public $user_expert;
    public $expert_follow_count;

    /**
     *
     * @var double
     */
    public $user_money;

    /**
     *
     * @var integer
     */
    public $user_money_virtual;

    /**
     *
     * @var integer
     */
    public $user_state;

    /**
     *
     * @var string
     */
    public $user_addtime;
	/**
	 * 
	 * @var unknown
	 */
    public $is_manage;
    /**
     * 
     * @var unknown
     */
	public $bind_phone;
	/**
	 * 
	 * @var unknown
	 */
	public $persional_sign;
    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->hasMany('user_id', 'Apps\Common\Models\Question', 'user_id', array('alias' => 'Question'));
        $this->hasMany('user_id', 'Apps\Common\Models\QuestionAnswer', 'user_id', array('alias' => 'QuestionAnswer'));
        $this->hasMany('user_id', 'Apps\Common\Models\QuestionAnswerFollow', 'user_id', array('alias' => 'QuestionAnswerFollow'));
        $this->hasMany('user_id', 'Apps\Common\Models\QuestionFollow', 'user_id', array('alias' => 'QuestionFollow'));
        $this->hasMany('user_id', 'Apps\Common\Models\UserAttribute', 'user_id', array('alias' => 'UserAttribute'));
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'user_base';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return UserBase[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return UserBase
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return array(
            'user_id' => 'user_id',
            'user_account' => 'user_account',
            'user_password' => 'user_password',
            'user_nickname' => 'user_nickname',
            'user_cover' => 'user_cover',
            'user_gender' => 'user_gender',
            'user_birthday' => 'user_birthday',
            'user_realname' => 'user_realname',
            'user_expert' => 'user_expert',
            'expert_follow_count' => 'expert_follow_count',
            'user_district' => 'user_district',
            'user_money' => 'user_money',
            'user_money_virtual' => 'user_money_virtual',
            'user_state' => 'user_state',
            'user_addtime' => 'user_addtime',
        	'is_manage'=>'is_manage',
            'persional_sign'=>'persional_sign',
        	'bind_phone'=>'bind_phone'
        );
    }

    public function defaultColumns()
    {
        return [
            'user_cover',
            'user_gender',
            'user_birthday',
            'user_realname',
            'user_expert',
            'user_district',
            'user_money',
            'user_money_virtual',
            'user_state',
            'user_addtime',
            'is_manage'
        ];
    }

    /**
     *
     * 设置手机号码
     *
     * @param $userId
     * @param $mobile
     * @return bool
     */
    public static function SetMobile($userId, $mobile)
    {
        $sql = 'UPDATE user_base SET bind_phone = :mobile, user_account = ' . $mobile . ' WHERE user_id = :uid';
        $model = new self;
        return $model->getWriteConnection()->execute($sql, ['mobile' => $mobile, 'uid' => $userId]);
    }

    /**
     * 设置密码
     * @param $mobile
     * @param $password
     * @return bool
     */
    public static function SetPassword($mobile, $password)
    {
        $sql = 'UPDATE user_base SET user_password = :pwd WHERE user_account = :mobile';
        $model = new self;
        return $model->getWriteConnection()->execute($sql, ['pwd' => $password, 'mobile' => $mobile]);
    }

    /**
     * 获取一个用户的信息
     * @param int $user_id 用户id
     * @return array
     */
    public static function getOne($user_id)
    {
        if ($model = self::findFirst('user_id = ' . $user_id)) {
            return $model->toArray();
        }
        return [];
    }

    /**
     * 用户是否是专家
     * @param $user_id
     * @return bool
     */
    public function is_expert($user_id)
    {
        return $this->getOne($user_id)['user_expert'] == 1 ? true : false;
    }

    /**
     * 修改专家的粉丝数
     * @param int $user_id 专家的id（用户的id）
     * @param int $count 增/减的数量
     * @param bool $type true-增，false-减
     */
    public function setExpertFollowCount($user_id, $count = 1, $type = true)
    {
        if ($type) {
            (new self)->getReadConnection()->query("UPDATE user_base SET expert_follow_count = expert_follow_count + {$count} WHERE user_id = {$user_id}");
        } else {
            (new self)->getReadConnection()->query("UPDATE user_base SET expert_follow_count = expert_follow_count - {$count} WHERE user_id = {$user_id}");
        }
    }
}
